package com.example.pixelyear.utils.dialog

import android.content.Context
import android.os.Bundle
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog
import com.example.pixelyear.R
import com.example.pixelyear.utils.Utils
import com.socks.library.KLog

abstract class BaseDialog(ctx: Context) : AlertDialog(ctx, R.style.CustomDialog) {
    lateinit var rootView: View


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setCancelable(true)//是否可以取消 (也可以在调用处设置)
        setCanceledOnTouchOutside(true)//是否点击外部消失
        rootView = View.inflate(context, getLayoutID(), null)
        setContentView(rootView)

        val params = window?.attributes
        params?.width = (Utils.getWindowWidth(context) * windowWidth()).toInt()
        KLog.e("yxs","弹窗高度："+windowHeight())
        params?.height = if (windowHeight() == 0f) WindowManager.LayoutParams.WRAP_CONTENT
        else (Utils.getWindowHeight(context) * windowHeight()).toInt()


        window?.attributes = params//设置显示的大小
        window?.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)

        initAny()
    }

    abstract fun getLayoutID(): Int

    abstract fun initAny()


    open fun windowWidth(): Float {
        return 0.8f
    }

    open fun windowHeight(): Float = 0f

}
